package com.weme.common.dialog

import account_server.v1.Account
import account_server.v1.Account.Goods
import android.content.Context
import android.graphics.Paint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.PagerSnapHelper
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.QuickViewHolder
import com.jeremyliao.liveeventbus.LiveEventBus
import com.weme.common.R
import com.weme.common.base.BaseBottomDialogFragment
import com.weme.common.databinding.DialogRechargeBinding
import com.weme.common.util.LiveConfig.NET_WORK_GOODS
import com.weme.common.util.loadImage
import com.weme.common.util.showToast
import com.weme.common.viewmodel.GiftViewMode
import dev.kratos.api.user_server.v1.GiftInfo


interface RechargeDialogCallback {
    fun sendGiftInfo(giftInfo: GiftInfo)
}
class RechargeDialog(private val callback: RechargeDialogCallback) : BaseBottomDialogFragment() {
    val dataList = mutableListOf<Goods>()
    private lateinit var binding: DialogRechargeBinding
    private var viewModel: GiftViewMode? = null
    companion object {
        // 创建并显示 RechargeDialog 实例的静态方法
        fun show(context: Context, callback: RechargeDialogCallback): RechargeDialog {
            val dialog = RechargeDialog(callback)
            dialog.show((context as AppCompatActivity).supportFragmentManager, "RechargeDialog")
            return dialog
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        binding = DialogRechargeBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // binding = DialogGiftBinding.inflate(LayoutInflater.from(context), null, false) //   giftAdapter = GiftAdapter(requireContext(),listOf("", "", "", "", ""))
        //  binding.banner.setAdapter(giftAdapter,false).setIndicator(CircleIndicator(requireContext())).addBannerLifecycleObserver(this)
        viewModel = ViewModelProvider(this)[GiftViewMode::class.java]




        val pagerSnapHelper = PagerSnapHelper()
        pagerSnapHelper.attachToRecyclerView(binding.rcvList)
        binding.rcvList.adapter = rechargeAdapter
        viewModel?.getGoodsList(Account.GetGoodsListRequest.newBuilder().build())
     LiveEventBus.get<Any>(NET_WORK_GOODS).observe(this){
            if(it is Account.GetGoodsListReply){
                dataList.addAll(it.goodsList)
            }
        }

        rechargeAdapter.submitList(dataList)
        rechargeAdapter.setOnItemClickListener{ _, _, position ->
            rechargeAdapter.getItem(position)?.let {
                showToast(it.goodName)
            }

        }
    }

    private val rechargeAdapter = object : BaseQuickAdapter<Goods, QuickViewHolder>() {
        override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: Goods?) {
            item?.apply {
               val tvOldMoney = holder.getView<TextView>(R.id.tvOldMoney)
                tvOldMoney.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
                tvOldMoney.paint.isAntiAlias = true
                holder.setText(R.id.tvName,this.goodName.toString())
                tvOldMoney.text = this.discount.toString()
                val imView = holder.getView<ImageView>(R.id.imView)
                imView.loadImage(this.pic)
                holder.setText(R.id.tvMoney,this.price.toString())

            }

        }

        override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
            return QuickViewHolder(R.layout.item_dialog_recharge, parent)
        }


    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.TUIBeautyDialogTheme)
    }




    override fun onDestroy() {
        super.onDestroy()

    }

}